package com.eastrobot.doc.poitlconvert.util;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * @author god23bin
 * @created 2023/6/25 23:28
 */
public class WordUtil {

    /**
     * 生成word，并浏览器下载word
     * @param template
     * @param response
     */
    public static void downloadServlet(XWPFTemplate template, HttpServletResponse response){
        try{
            response.setHeader("Content-disposition", "attachment;filename=" + new String("model".getBytes("gb2312"), "ISO8859-1") + ".docx");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            response.setContentType("multipart/form-data;charset=UTF-8");
            ServletOutputStream out = response.getOutputStream();
            template.write(out);
            out.flush();
            template.close();
            out.close();
        }catch (Exception e){
            e.getMessage();
        }
    }


    /**
     * 生成 word 文档
     * @param wordTemplatePath    word 模板路径
     * @param targetWordFilePath  生成目标文档路径
     * @param data                待渲染的数据模型-哈希表形式
     */
    public static void generateWordFile(String wordTemplatePath, String targetWordFilePath, Map<String, Object> data) {
        XWPFTemplate template = XWPFTemplate.compile(wordTemplatePath).render(data);
        try {
            template.writeAndClose(new FileOutputStream(targetWordFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成 word 文档
     * @param wordTemplatePath    word 模板路径
     * @param targetWordFilePath  生成目标文档路径
     * @param data                待渲染的数据模型-Java对象形式
     */
    public static void generateWordFile(String wordTemplatePath, String targetWordFilePath, Object data) {
        XWPFTemplate template = XWPFTemplate.compile(wordTemplatePath).render(data);
        try {
            template.writeAndClose(new FileOutputStream(targetWordFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成 word 文档
     * @param wordTemplatePath    word 模板路径
     * @param targetWordFilePath  生成目标文档路径
     * @param data                待渲染的数据模型-哈希表形式
     * @param configure           渲染配置
     */
    public static void generateWordFile(String wordTemplatePath, String targetWordFilePath, Map<String, Object> data, Configure configure) {
        XWPFTemplate template = XWPFTemplate.compile(wordTemplatePath, configure).render(data);
        try {
            template.writeAndClose(new FileOutputStream(targetWordFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成 word 文档
     * @param wordTemplatePath    word 模板路径
     * @param targetWordFilePath  生成目标文档路径
     * @param data                待渲染的数据模型-Java对象形式
     * @param configure           渲染配置
     */
    public static void generateWordFile(String wordTemplatePath, String targetWordFilePath, Object data, Configure configure) {
        XWPFTemplate template = XWPFTemplate.compile(wordTemplatePath, configure).render(data);
        try {
            template.writeAndClose(new FileOutputStream(targetWordFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
